/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "icache.h"
#include "memorymap.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
#include "bsp_ili9341_4line.h"
#include "bsp_sht20.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#include<string.h>
#include "lvgl.h"                // 它为整个LVGL提供了更完整的头文件引用
#include "lv_port_disp.h"        // LVGL的显示支持
#include "lv_port_indev.h"       // LVGL的触屏支持
#include "bsp_ft6336.h"

int fputc(int ch,FILE * p)
{
    while(!(USART1->ISR & (1<<7)));
    USART1->TDR = ch;
    return ch;
}
/* USER CODE BEGIN 0 */
//定义一个保存了所有数字显示状态的数组，显示：0、1、2、3、4、5、6、7、8、9
const uint8_t number[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

//定义一个用于保存想要显示的数字的变量，volatile使变量不被优化，每次都刷新读取
volatile uint16_t show_number = 0x1234;
uint8_t buf[4]="0000";

int _write(int file, char *ptr, int len)
{
    (void)file;
    HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, 0xffff);
    return len;
}

/* USER CODE END 0 */

char str[64];
extern volatile SHT20_TemRH_Val gTemRH_Val;
int flat=1;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
const unsigned char gImage_11[800] = { /* 0X10,0X10,0X00,0X14,0X00,0X14,0X01,0X1B, */
        0XFF,0XBE,0XF7,0XDF,0XEF,0XDF,0XF7,0XFF,0XEF,0XFF,0XE7,0XFF,0XE7,0XFF,0XEF,0XFF,
        0XEF,0XFF,0XE7,0XFF,0XDF,0XFF,0XDF,0XFF,0XD7,0XFF,0XDF,0XFF,0XE7,0XFF,0XEF,0XFF,
        0XF7,0XFF,0XEF,0XBF,0XFF,0XDE,0XFF,0X7D,0XFF,0X9E,0XF7,0XFF,0XE7,0XFF,0X9E,0X3B,
        0X6C,0XB6,0X64,0X96,0X64,0X77,0X5C,0X37,0X64,0X37,0X64,0X78,0X54,0X57,0X54,0X77,
        0X5C,0X97,0X5C,0X56,0X5C,0X57,0X74,0XB8,0XA6,0X5C,0XEF,0XFF,0XF7,0XBF,0XFF,0XDE,
        0XFF,0XBF,0XE7,0XBF,0X6D,0X1A,0X12,0XD3,0X1B,0X15,0X13,0X15,0X13,0X16,0X13,0X16,
        0X13,0X16,0X13,0X16,0X13,0X16,0X13,0X16,0X13,0X16,0X1B,0X36,0X13,0X15,0X12,0XF4,
        0X12,0XB3,0X6C,0XD9,0XEF,0XFF,0XEF,0XDF,0XFF,0XFF,0XA6,0X1C,0X12,0XD4,0X13,0X79,
        0X13,0X59,0X13,0X79,0X13,0X59,0X13,0X78,0X13,0X78,0X13,0X78,0X1B,0X78,0X1B,0X79,
        0X1B,0X79,0X13,0X59,0X13,0X79,0X13,0X99,0X1B,0X78,0X1A,0XF4,0X9E,0X1C,0XEF,0XFF,
        0XEF,0XFF,0X74,0XD8,0X0A,0XD5,0X13,0X7A,0X13,0X7B,0X13,0X59,0X1B,0X57,0X13,0X15,
        0X1B,0X14,0X1B,0X13,0X23,0X14,0X1A,0XF4,0X12,0XF6,0X13,0X38,0X0B,0X79,0X0B,0X79,
        0X0B,0X79,0X0B,0X15,0X6C,0XD8,0XE7,0XFF,0XEF,0XFF,0X5C,0X56,0X13,0X36,0X13,0X79,
        0X13,0X39,0X13,0X17,0X2B,0X76,0X65,0X3B,0X9E,0XDF,0XBF,0X9F,0XBF,0X9F,0XA6,0XDF,
        0X6D,0X5D,0X2B,0X77,0X13,0X16,0X13,0X78,0X0B,0XBA,0X0B,0X16,0X5C,0X97,0XE7,0XFF,
        0XE7,0XFF,0X64,0X56,0X13,0X16,0X13,0X58,0X1B,0X55,0X54,0XB9,0XCF,0XFF,0XD7,0XFF,
        0XB7,0X1F,0X8D,0XFD,0X95,0XFD,0XB6,0XDF,0XE7,0XFF,0XCF,0XDF,0X5C,0XD9,0X1B,0X34,
        0X13,0X97,0X13,0X35,0X5C,0X56,0XE7,0XFF,0XEF,0XFF,0X5C,0X56,0X13,0X36,0X0B,0X37,
        0X54,0XFB,0XCF,0XFF,0X9E,0X5E,0X53,0XD3,0X64,0X57,0X85,0X9C,0X85,0X9B,0X6C,0X96,
        0X5B,0XB2,0XA6,0X5D,0XD7,0XFF,0X5C,0XFB,0X0B,0X15,0X13,0X14,0X64,0X77,0XE7,0XFF,
        0XEF,0XFF,0X5C,0X56,0X13,0X36,0X0B,0X79,0X34,0X1A,0X65,0X1C,0X54,0X16,0XC7,0X3F,
        0XDF,0XFF,0XC7,0XBF,0XC7,0X9F,0XDF,0XFF,0XCF,0X7F,0X53,0XF5,0X65,0X3D,0X3C,0X3B,
        0X13,0X57,0X1B,0X15,0X5C,0X56,0XEF,0XFF,0XDF,0XFF,0X64,0X77,0X0B,0X16,0X0B,0X59,
        0X1B,0X58,0X12,0X94,0XB7,0X3F,0XBF,0X3F,0X54,0X15,0X5C,0X35,0X64,0X55,0X54,0X15,
        0XB7,0X1F,0XAF,0X3F,0X0A,0XD5,0X13,0X38,0X13,0X58,0X1B,0X16,0X64,0X56,0XEF,0XFF,
        0XE7,0XFF,0X5C,0X57,0X1B,0X36,0X13,0X79,0X1B,0X58,0X1B,0X57,0X2B,0X77,0X3B,0XB6,
        0XB7,0X1F,0XE7,0XFF,0XE7,0XFF,0XB6,0XFF,0X3B,0XD7,0X2B,0XD9,0X13,0X58,0X13,0X79,
        0X13,0X79,0X13,0X36,0X5C,0X77,0XE7,0XFF,0XE7,0XFF,0X5C,0X37,0X13,0X15,0X13,0X78,
        0X13,0X58,0X1B,0X78,0X13,0X37,0X33,0XB8,0XA6,0X7F,0X64,0X34,0X6C,0X54,0XA6,0X7F,
        0X33,0XF9,0X0B,0X18,0X13,0X58,0X13,0X79,0X13,0X79,0X13,0X36,0X5C,0X77,0XE7,0XFF,
        0XE7,0XFF,0X64,0X78,0X13,0X14,0X13,0X98,0X13,0X98,0X13,0X78,0X13,0X79,0X1B,0X36,
        0X33,0X33,0XB6,0XBF,0XAE,0X7F,0X3B,0X74,0X13,0X16,0X13,0X99,0X13,0X99,0X13,0X99,
        0X0B,0X99,0X13,0X36,0X5C,0X77,0XE7,0XFF,0XE7,0XFF,0X54,0X57,0X13,0X35,0X13,0X78,
        0X13,0X78,0X13,0X78,0X13,0X99,0X13,0X16,0X64,0XFC,0XD7,0XFF,0XDF,0XFF,0X6D,0X3C,
        0X0B,0X16,0X0B,0X78,0X13,0X58,0X13,0X58,0X13,0X79,0X13,0X15,0X5C,0X77,0XE7,0XFF,
        0XDF,0XFF,0X5C,0X97,0X13,0X15,0X1B,0X99,0X13,0X79,0X13,0X79,0X0B,0X59,0X13,0X58,
        0X33,0XD8,0X9E,0XDF,0X9E,0XFF,0X2B,0XB7,0X13,0X77,0X13,0X99,0X13,0X59,0X1B,0X79,
        0X13,0X59,0X13,0X15,0X64,0X77,0XEF,0XFF,0XE7,0XFF,0X6C,0XD8,0X12,0XF5,0X13,0X59,
        0X13,0X7A,0X0B,0X59,0X1B,0X9A,0X13,0X59,0X13,0X37,0X1B,0X37,0X13,0X36,0X1B,0X77,
        0X13,0X78,0X13,0X79,0X1B,0X79,0X13,0X59,0X1B,0X59,0X1B,0X15,0X6C,0X97,0XEF,0XFF,
        0XF7,0XFF,0XA6,0X1C,0X1A,0XB4,0X13,0X59,0X0B,0X7A,0X0B,0X79,0X1B,0X79,0X1B,0X58,
        0X1B,0X59,0X13,0X58,0X13,0X58,0X13,0X78,0X13,0X79,0X13,0X79,0X13,0X79,0X13,0X59,
        0X1B,0X79,0X12,0XB4,0XA6,0X3D,0XEF,0XFF,0XF7,0XBE,0XEF,0XFF,0X6C,0XBA,0X12,0XB5,
        0X0A,0XF6,0X0B,0X16,0X1B,0X15,0X1A,0XF5,0X1B,0X16,0X13,0X16,0X13,0X15,0X13,0X15,
        0X13,0X16,0X13,0X16,0X13,0X16,0X13,0X15,0X0A,0X93,0X64,0XDA,0XE7,0XFF,0XEF,0XDF,
        0XFF,0XBD,0XFF,0XBE,0XEF,0XFF,0X9E,0X3D,0X6C,0XF8,0X5C,0X97,0X5C,0X36,0X64,0X57,
        0X5C,0X77,0X5C,0X77,0X5C,0X77,0X5C,0X77,0X5C,0X77,0X5C,0X57,0X5C,0X77,0X64,0X97,
        0XA6,0X5D,0XEF,0XFF,0XF7,0XFF,0XFF,0XDE,0XFF,0X7C,0XFF,0XDE,0XF7,0XBF,0XEF,0XFF,
        0XE7,0XFF,0XDF,0XFF,0XEF,0XFF,0XE7,0XFF,0XE7,0XFF,0XDF,0XFF,0XE7,0XFF,0XE7,0XFF,
        0XE7,0XFF,0XDF,0XFF,0XE7,0XFF,0XEF,0XFF,0XEF,0XFF,0XEF,0XBE,0XFF,0XBD,0XFF,0X9D,
};



/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//定义一个保存了所有数字显示状态的数组，显示：0、1、2、3、4、5、6、7、8、9
static void myBtn_event(lv_event_t *event)
{
    lv_obj_t *btn = lv_event_get_target(event);                    // 获得调用这个回调函数的对象
    if (event->code == LV_EVENT_CLICKED)
    {
        static uint8_t cnt = 0;
        cnt++;
        lv_obj_t *label = lv_obj_get_child(btn, (int32_t) NULL);             // 获取第1个子对象(我们在设计时，已安排了它的第1个子对象是一个label对象)
        lv_label_set_text_fmt(label, "Button: %d", cnt);           // 设置标签的文本，写法类似printf
//        printf("ck is sb\n");
    }
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_SPI1_Init();
  MX_I2C1_Init();
  MX_SPI2_Init();
  MX_TIM6_Init();
  MX_ICACHE_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

    ILI9341_Init();//静态图
    ILI9341_Clear(WHITE);


    lv_init();                             // LVGL 初始化
    lv_port_disp_init();                   // 注册LVGL的显示任务
    lv_port_indev_init();                  // 注册LVGL的触屏检测任务
    // 按钮
    lv_obj_t *myBtn = lv_btn_create(lv_scr_act());                               // 创建按钮; 父对象：当前活动屏幕
    lv_obj_set_pos(myBtn, 10, 10);                                               // 设置坐标
    lv_obj_set_size(myBtn, 120, 50);                                             // 设置大小
    lv_obj_add_event_cb(myBtn, myBtn_event, LV_EVENT_ALL, NULL);
// 按钮的事件回调函数

    // 按钮上的文本
    lv_obj_t *label_btn = lv_label_create(myBtn);                                // 创建文本标签，父对象：上面的btn按钮
    lv_obj_align(label_btn, LV_ALIGN_CENTER, 0, 0);                              // 对齐于：父对象
    lv_label_set_text(label_btn, "Test");                                        // 设置标签的文本

    // 独立的标签
    lv_obj_t *myLabel = lv_label_create(lv_scr_act());                           // 创建文本标签; 父对象：当前活动屏幕
    lv_label_set_text(myLabel, "Hello world!");                                  // 设置标签的文本
    lv_obj_align(myLabel, LV_ALIGN_CENTER, 0, 0);                                // 对齐于：父对象
    lv_obj_align_to(myBtn, myLabel, LV_ALIGN_OUT_TOP_MID, 0, -20);               // 对齐于：某对象
    HAL_TIM_Base_Start_IT(&htim6);
//    LCD_Picture_pic(10,20,20,40,gImage_11);
//    Gui_DrawFont_GBK16(120,160,BLACK,0x8010,"nihao");
//char buf[128];
    HAL_UART_Receive_IT(&huart1, (uint8_t *)buf, 4);
    while (1) {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

        HAL_Delay(1-1);
        static uint8_t  msLVGL=0;
        if (msLVGL++>=5)
        {
            lv_timer_handler();
            msLVGL=0;
        }

    }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_0;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV4;
  RCC_OscInitStruct.PLL.PLLM = 3;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 1;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1;
  RCC_OscInitStruct.PLL.PLLFRACN = 0;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
    if(!(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_5))&(GPIO_Pin==GPIO_PIN_5))
    {
//        FT6336_scan();
//        FT6336_scan();

    }


    extern int flat;
    if (GPIO_Pin == GPIO_PIN_8)
    {
        uint8_t msg1[] = "GPIO_PIN_8"; // 使用 uint8_t 数组避免类型问题
        HAL_UART_Transmit(&huart1, msg1, sizeof(msg1) - 1, 1000);

        HAL_GPIO_TogglePin(fan_GPIO_Port, fan_Pin);
        HAL_GPIO_TogglePin(motor_GPIO_Port,  motor_Pin);


    }
    if (GPIO_Pin == GPIO_PIN_9)
    {
        uint8_t msg1[] = "GPIO_PIN_9"; // 使用 uint8_t 数组避免类型问题
        flat = (flat+1)%2;
        HAL_UART_Transmit(&huart1, msg1, sizeof(msg1) - 1, 1000);

    }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim->Instance == TIM6)
    {

        lv_tick_inc(1);
    }
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1) {
    }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
